﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Windows.Forms;


namespace Liang.UI
{
      /// <summary>
    /// utGridView中的RowFlag标识设计器
    /// </summary>
    public class DataGridViewFlagRowEditor : UITypeEditor
    {
        const string NullValue = "(None)";
        private IWindowsFormsEditorService m_editorService = null;
        public DataGridViewFlagRowEditor()
            : base()
        {
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
            IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null)
            {
                m_editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
                DataGridViewCheckBoxColumnEx column = null;
                List<string> columnNameList = new List<string>();

                foreach (IComponent c in context.Container.Components)
                {
                    if (c is DataGridViewCheckBoxColumnEx)
                    {
                        column = (DataGridViewCheckBoxColumnEx)c;
                        columnNameList.Add(column.Name);
                    }
                }
                if (columnNameList.Count > 0 && m_editorService != null)
                {
                    int i = 0;
                    string selectValue = (string)value;
                    ListBox listBox = new ListBox();
                    listBox.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged);
                    listBox.Items.Add(NullValue);
                    foreach (string name in columnNameList)
                    {
                        i = listBox.Items.Add(name);
                        if (name == selectValue)
                            listBox.SelectedIndex = i;
                    }
                    m_editorService.DropDownControl(listBox);
                    if (listBox.SelectedItem != null)
                    {
                        if (listBox.SelectedItem.ToString() == NullValue)
                            return string.Empty;
                        return listBox.SelectedItem;
                    }
                }
            }
            return value;
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_editorService != null)
                m_editorService.CloseDropDown();
        }

        /// <summary>
        /// Gets the editor style used by the EditValue method.
        /// </summary>
        /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
        /// <returns>A UITypeEditorEditStyle value that indicates the style of editor used by EditValue. If the UITypeEditor does not support this method, then GetEditStyle will return None</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }
    }
}
